package com.zjdiepu.www.biz.yw;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.diepu.www.client.partner.proxy.response.DiepuYwInterviewInfo;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.status.InterviewStatus;
import com.zjdiepu.www.base.enums.status.PushStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.consts.SystemConsts;
import com.zjdiepu.www.dao.yw.YwBusinessMapper;
import com.zjdiepu.www.dao.yw.YwInterviewInfoMapper;
import com.zjdiepu.www.ibiz.yw.IYwInterviewInfoService;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.dto.YwInterviewInfo;
import com.zjdiepu.www.model.yw.ext.YwInterviewInfoExt;
import com.zjdiepu.www.security.util.CollectionInitUtil;
import com.zjdiepu.www.util.ExceptionHelper;

/**
 * yw_面签信息Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2018年03月14日 17时29分<br/>
 * @since 1.0<br/>
 */
@Service
public class YwInterviewInfoServiceImpl extends BaseService implements IYwInterviewInfoService {
	public static final Logger logger = LoggerFactory.getLogger(YwInterviewInfoServiceImpl.class);

	@Autowired
	private YwInterviewInfoMapper ywInterviewInfoMapper;
	@Autowired
	private YwBusinessMapper ywBusinessMapper;
	
	@Override
	public void saveCheckSecondInterview(String interviewInfoId, String busiId,	AbstractSysUser currentUser) {
		YwInterviewInfo ywInterviewInfo = new YwInterviewInfo();
		ywInterviewInfo.initUpdateData(currentUser.getId(), currentUser.getName());
		ywInterviewInfo.setId(interviewInfoId);
		ywInterviewInfo.setInterviewStatus(InterviewStatus.MIAN_QIAN_CHENG_GONG.getCode());
		int count = ywInterviewInfoMapper.updateByPrimaryKeySelective(ywInterviewInfo);
		if(count > 1) {
			throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
		} else if(count == 0) {
			throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
		}
		YwBusiness ywBusiness = new YwBusiness();
		ywBusiness.setId(busiId);
		ywBusiness.setInterviewStatus(InterviewStatus.MIAN_QIAN_CHENG_GONG.getCode());
		count = ywBusinessMapper.updateByPrimaryKeySelective(ywBusiness);
		if(count > 1) {
			throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
		} else if(count == 0) {
			throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
		}
	}
	
	@Override
	public void saveDownloadStatus(String busiId, Integer downloadStatus, String urlLocal) {
		YwInterviewInfo ywInterviewInfo = new YwInterviewInfo();
		ywInterviewInfo.setDpStatus(DpStatus.NORMAL.getCode());
		ywInterviewInfo.setBusiId(busiId);
		ywInterviewInfo = ywInterviewInfoMapper.selectRefined(ywInterviewInfo);
		if(null == ywInterviewInfo) {
			logger.error("面签信息记录丢失.业务ID：{}，下载状态：{}。",busiId , downloadStatus);
			ExceptionHelper.thow(ResultFlagType.MISSING.getCode().toString(), "面签信息记录丢失");
		}
		ywInterviewInfo.setDownloadStatus(downloadStatus);
		ywInterviewInfo.setUrlLocal(urlLocal);
		ywInterviewInfo.initUpdateData(SystemConsts.DEFAULT_ID, "同步面签下载状态");
		ywInterviewInfoMapper.updateByPrimaryKeySelective(ywInterviewInfo);
	}
	
	@Override
	public void savePushStatus(String busiId, Integer pushStatus) {
		YwInterviewInfo ywInterviewInfo = new YwInterviewInfo();
		ywInterviewInfo.setDpStatus(DpStatus.NORMAL.getCode());
		ywInterviewInfo.setBusiId(busiId);
		ywInterviewInfo = ywInterviewInfoMapper.selectRefined(ywInterviewInfo);
		if(null == ywInterviewInfo) {
			logger.error("面签信息记录丢失.业务ID：{}，推送状态：{}。",busiId , pushStatus);
			ExceptionHelper.thow(ResultFlagType.MISSING.getCode().toString(), "面签信息记录丢失");
		}
		ywInterviewInfo.setPushStatus(pushStatus);
		ywInterviewInfo.initUpdateData(SystemConsts.DEFAULT_ID, "同步面签推送状态");
		ywInterviewInfoMapper.updateByPrimaryKeySelective(ywInterviewInfo);
	}
	
	@Override
	public List<DiepuYwInterviewInfo> synInterviewInfo() {
		YwInterviewInfoExt ywInterviewInfoExt = new YwInterviewInfoExt();
		ywInterviewInfoExt.setBaseWhere();
		ywInterviewInfoExt.withOutPage();
		ywInterviewInfoExt.setWhereSql("`URL_QINIU` IS NOT NULL AND `URL_QINIU` != '' AND `PUSH_STATUS` != " + PushStatus.YI_TUI_SONG.getCode());
		List<YwInterviewInfo> ywInterviewInfos = ywInterviewInfoMapper.selectByExt(ywInterviewInfoExt);
		List<DiepuYwInterviewInfo> interviewInfos = CollectionInitUtil.emptyList();
		for(YwInterviewInfo ywInterviewInfo : ywInterviewInfos) {
			DiepuYwInterviewInfo diepuYwInterviewInfo = new DiepuYwInterviewInfo();
			BeanUtils.copyProperties(ywInterviewInfo, diepuYwInterviewInfo);
			interviewInfos.add(diepuYwInterviewInfo);
		}
		return interviewInfos;
	}
	
	@Override
	public void saveUpdateAndDelete(YwInterviewInfo ywInterviewInfo, OperateType operType) {
		switch (operType) {
		case add:
			ywInterviewInfo.initInsertData(ywInterviewInfo.getModifier(), ywInterviewInfo.getModifierName());
			ywInterviewInfoMapper.insert(ywInterviewInfo);
			break;
		case edit:
			ywInterviewInfo.initUpdateData(ywInterviewInfo.getModifier(), ywInterviewInfo.getModifierName());
			// 影响的记录数
			int updateCount = ywInterviewInfoMapper.updateByPrimaryKeySelective(ywInterviewInfo);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			ywInterviewInfo.initDeleteData(ywInterviewInfo.getModifier(), ywInterviewInfo.getModifierName());
			// 影响的记录数
			int deleteCount = ywInterviewInfoMapper.updateByPrimaryKeySelective(ywInterviewInfo);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<YwInterviewInfo> selectByExt(YwInterviewInfoExt ywInterviewInfoExt) {
		return ywInterviewInfoMapper.selectByExt(ywInterviewInfoExt);
	}
	
	@Override
	public int countByExt(YwInterviewInfoExt ywInterviewInfoExt){
		return ywInterviewInfoMapper.countByExt(ywInterviewInfoExt);
	}
	
	@Override
	public YwInterviewInfo selectById(String id) {
		return ywInterviewInfoMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			ywInterviewInfoMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(YwInterviewInfo ywInterviewInfo) {
		return ywInterviewInfoMapper.countRefined(ywInterviewInfo);
	}

	@Override
	public YwInterviewInfo selectRefined(YwInterviewInfo ywInterviewInfo) {
		return ywInterviewInfoMapper.selectRefined(ywInterviewInfo);
	}
	
	@Override
	public int deletedRefined(YwInterviewInfo ywInterviewInfo) {
		return ywInterviewInfoMapper.deletedRefined(ywInterviewInfo);
	}

	@Override
	public boolean repeat(YwInterviewInfo ywInterviewInfo) {
		int count = countRefined(ywInterviewInfo);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(ywInterviewInfo.getId())) {
			return true;
		}
		YwInterviewInfo dbItem = selectRefined(ywInterviewInfo);
		if(null == dbItem) {
			return false;
		}
		return !ywInterviewInfo.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return ywInterviewInfoMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<YwInterviewInfo> ywInterviewInfos) {
		return ywInterviewInfoMapper.insertBatch(ywInterviewInfos);
	}
}
